;------------------------------------------------------------------------------------------------------
; Module4: A Simple SoC  Application
; Immediately echo back each received character from UART.
;------------------------------------------------------------------------------------------------------

                        PRESERVE8
                        THUMB

                        AREA RESET, DATA, READONLY  ; First 32 WORDS is VECTOR TABLE
                        EXPORT __Vectors
					
__Vectors               DCD     0x0000FFFC            ; 65532 Internal Memory
                        DCD     Reset_Handler
                        DCD     0  			
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
        				
                        ; External Interrupts
						        				
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
                        DCD     0
              
                AREA |.text|, CODE, READONLY
; Reset Handler
Reset_Handler   PROC
                GLOBAL Reset_Handler
                ENTRY

ECHO_LOOP       ; 等待接收字符
WAIT_RX         LDR     R1, =0x53000004         ; 加载 UART 状态寄存器地址到 R1
                LDR     R0, [R1]                ; 从地址 R1 加载状态寄存器数据到 R0
                MOVS    R1, #1                  ; 将值 0x01 加载到 R1
                ANDS    R0, R0, R1              ; 保留 R0 的最低位，其他位置 0
                CMP     R0, #0                  ; 比较 R0 是否为 0
                BNE     WAIT_RX                 ; 如果 R0 不为 0，跳转到 WAIT_RX

                ; 接收并立即回传字符
                LDR     R1, =0x53000000         ; 加载 UART 数据寄存器地址到 R1
                LDR     R0, [R1]                ; 从 UART 数据寄存器加载接收到的字符到 R0
                STR     R0, [R1]                ; 将 R0 中的字符立即发送回去

                B       ECHO_LOOP               ; 继续循环接收下一个字符

                ENDP                            ; 过程结束

                ALIGN   4                       ; 对齐到字边界

END                                             ; 程序结束
